package com.annhoa.domain.service;

import java.util.List;
import java.util.Map;

import com.annhoa.domain.entity.SysUser;
import com.annhoa.domain.repository.SysUserRepository;
import com.annhoa.domain.req.SysUserReq;
import com.annhoa.infrastructure.common.BasePage;
import com.annhoa.infrastructure.common.DAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.annhoa.domain.repository.SysUserJpaRepository;
import com.annhoa.infrastructure.common.PageData;

@Service
@Transactional
public class SysUserService {
    @Autowired
    private SysUserJpaRepository sysUserJpaRepository;
    @Autowired
    private SysUserRepository sysUserRepository;

    @Autowired
    private DAO dao;

    /*
     * 查找所有
     */
    public List<SysUser> findAll() {
        return sysUserJpaRepository.findAll();
    }

    /*
     * 用户名查找
     */
    public List<SysUser> findByName(String name) {
        return sysUserRepository.findByName(name);
    }

    /*
     * springdata保存
     */
    public void saveUser(SysUserReq req) {
        SysUser sysUser = new SysUser();
        sysUser.setName(req.getName());
        sysUser.setUsername(req.getUsername());
        sysUser.setAge(req.getAge());
        sysUser.setAddressId(req.getAddressId());
        sysUserJpaRepository.save(sysUser);
    }

    /*
     * id 查
     */
    @Cacheable("users")
    public SysUser findById(long id) {
        return sysUserJpaRepository.findById(id).get();
    }

    /*
     * id 删
     */
    public void delete(long id) {
        sysUserJpaRepository.deleteById(id);
    }

    public SysUser findUserByUsername(String username) {
        return sysUserRepository.findByUsername(username);
    }

    /**
     * name 查 排序
     *
     * @param name
     * @param sort
     * @return
     */
    public List<SysUser> findByName(String name, Sort sort) {
        return sysUserRepository.findByName(name, sort);
    }

    /*
     * 调用DaoSupport name 联表查询
     */
    @SuppressWarnings("unchecked")
    public List<PageData> findForList(PageData pd) throws Exception {
        String sql = "select u.* ,a.name address from sys_user u,sys_address a where a.address_id = u.address_id and u.name=:name"; // 注意语法
        // return (List<PageData>) daoSupport.queryListEntity(sql, pd,null);
        return (List<PageData>) dao.findForList(sql, pd, null);
    }

    /*
     * 调用DaoSupport id 查 return PageData
     */
    public PageData findForObject(PageData pd) throws Exception {
        String sql = "select * from sys_user where id=:id";
        return (PageData) dao.findForObject(sql, pd, null);
    }

    /*
     * 调用DaoSupport id 查 return List<User>
     */
    @SuppressWarnings("unchecked")
    public List<SysUser> findForList(SysUser sysUser) throws Exception {
        String sql = "select * from sys_user";
        return (List<SysUser>) dao.findForList(sql, sysUser, SysUser.class);
    }

    /*
     * @params pd 调用DaoSupport 保存
     */
    public void save(PageData pd) throws Exception {
        String sql = "insert into sys_user(id,username,password,name,address_id,age) values(:id,:username,:password,:name,:address_id,:age)";
        dao.save(sql, pd);
    }

    /**
     * 分页、排序
     *
     * @param page
     * @return
     * @throws Exception
     */

    @SuppressWarnings({"rawtypes"})
    public Page<Map> pageList(BasePage page) throws Exception {
        String sql = "select * from sys_user order by age";
        page.setSql(sql);
        return dao.findPageByParams(page);
    }

    public Page<SysUser> pageAll(Pageable var) {
        return sysUserJpaRepository.findAll(var);
    }
}